import 'dart:async';

import 'package:chop/Model/LoginEntity.dart';
import 'package:chop/common/SPUtils.dart';
import 'package:chop/widgets/Textwidget.dart';
import 'package:chop/widgets/ToastWidget.dart';
import 'package:chop/widgets/appBar.dart';
import 'package:chop/widgets/loadingDialog.dart';
import 'package:flutter/material.dart';
import 'package:chop/common/StringUtils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:chop/common/DioUtils.dart';
class Set_phone_number extends StatefulWidget {
  @override
  _Set_phone_numberState createState() => _Set_phone_numberState();
}

class _Set_phone_numberState extends State<Set_phone_number> {
  String _phone ;
  bool isNetWorkBad = false;
  TextEditingController _telephone_number;//新手机号
  TextEditingController userController;
  TextEditingController passwordController;
  TextEditingController codeController;
  bool isRider;
  Timer _timer;
  var _countdownTime = 0;
  @override
  void initState() {
    // TODO: implement initState
    getUser();
    super.initState();
    _telephone_number = TextEditingController();
  }
  getUser()async{
    _phone = await SPUtils.get("phone");

//    _ID = await SPUtils.get("phone");
    setState(() {
      isNetWorkBad = true;
    });
  }
  Widget build(BuildContext context) {

    return Scaffold(
      appBar:BaseViewBar(
        childView: BaseTitleBar(
          "换绑手机号",
        ),
        preferredSize: Size.fromHeight(65.0),
      ),
      body: Container(
                height: ScreenUtil().setHeight(600),
                padding: EdgeInsets.all(10),
                width: double.infinity,
//                color: Colors.cyan,
                child: Column(
                  children: <Widget>[
                    Container(
                     height: ScreenUtil().setHeight(100),
                     width: double.infinity,
//                     color: Colors.black12,
                     padding: EdgeInsets.fromLTRB(0, 20, 300, 0) ,
                     child: Column(
                       children: <Widget>[
                        Text(
                            '${_phone??"phone"}',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(35)
                          ),
                        ),
                       ],
                     ),
                    ),
                        Container(
                          height: ScreenUtil().setHeight(125),
                          margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
                          width: double.infinity,
                          child: Column(
                            children: <Widget>[
                              TextWidget(
                                controller: _telephone_number,
                                labelText: "新手机号",
                              ),
                            ],
                          ),
                        ),
                    Row(
                      children: <Widget>[
                        Expanded(
                          flex: 7,
                          child: TextField(
                              controller: codeController,
                              keyboardType: TextInputType.number,
                              style: TextStyle(
                                  fontSize:  ScreenUtil().setSp(25)
                              ),
                              decoration: InputDecoration(
                                contentPadding: EdgeInsets.all(10.0),
                                labelText: '请输入手机验证码',
                              ),
                              obscureText: true),
                        ),
                        Expanded(
                            flex: 3,
                            child:FlatButton(
                              splashColor: Colors.transparent,
                              highlightColor: Colors.transparent,
                              hoverColor: Colors.transparent,
                              padding: EdgeInsets.only(right: 0),
                              child: Text(handleCodeAutoSizeText(),
                                  style: TextStyle(
                                      color: Colors.black26,
                                      fontSize: 12)),
                              onPressed: () {
                                if(_countdownTime == 0){
                                  if(StringUtils.isNull(userController.text.trim()) || StringUtils.isNull(passwordController.text.trim())){
                                    Toast.toast(context,msg: "请填写手机号以及密码",bgColor: Colors.red);
                                  }else{
                                    showDialog<Null>(
                                      context: context, //BuildContext对象
                                      barrierDismissible: false,
                                      builder: (BuildContext context) {
                                        return LoadingDialog(
                                          //调用对话框
                                          text: '请稍后...',
                                        );
                                      },
                                    );
                                    DioUtils.login(isRider,username: userController.text.trim(),password: passwordController.text.trim()).then((LoginEntity result){
                                      Get.back();
                                      switch(result.code){
                                        case 0:
                                          startCountdown();
                                          Toast.toast(context,msg: "验证码已发送至您的手机");
                                          Timer.periodic(Duration(minutes: 10), (timer){
                                            SPUtils.remove("SMSCode");
                                          });
                                          SPUtils.save("SMSCode", result.sms);
                                          break;
                                        case 500:
                                          Toast.toast(context,msg:"${result.msg}",bgColor: Colors.red);
                                          break;
                                      }
                                      print(result.toJson());
                                    });
                                  }
                                }
                              },
                            )
                        )
                      ],
                    ),
                  ],
                ),
              ),

      
    );

  }
  String handleCodeAutoSizeText() {
    if (_countdownTime > 0) {
      return '${_countdownTime}s后重新发送';
    } else
      return '获取验证码';
  }
  startCountdown() {
    //倒计时时间
    _countdownTime = 60;
    final call = (timer) {
      if (_countdownTime < 1) {
        _timer.cancel();
      } else {
        setState(() {
          _countdownTime -= 1;
        });
      }
    };
    if (_timer == null) {
      _timer = Timer.periodic(Duration(seconds: 1), call);
    }
  }
}
